package dao;

import entry.Blog;
import entry.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    // 根据用户名查询对应用户信息
    public User getUserByName(String name){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from user where username=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);

            resultSet = statement.executeQuery();
            User user = new User();
            if (resultSet.next()){
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("password"));
                user.setPhoto(resultSet.getString("photo"));
                user.setGithub(resultSet.getString("github"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }


    // 根据用户 ID找到对应信息
    public User getUserById(int id){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from user where userId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);

            resultSet = statement.executeQuery();
            User user = new User();
            if (resultSet.next()){
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("userName"));
                user.setPassword("");
                user.setPhoto(resultSet.getString("photo"));
                user.setGithub(resultSet.getString("github"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 更新用户信息
    // 根据用户 ID更新用户
    public void updateUser(int userId, User user){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "";
            if (user.getPassword() != null && !user.getPassword().equals("")){
                // 是更新 用户名/密码/github
                sql = "update user set userName='" + user.getUsername() +
                        "',password='" + user.getPassword()+"', photo='"+user.getPhoto()+"',github=? where userId=?";
                String github = user.getGithub();
                if (github == null || github.equals("")){
                    github = null;
                }
                statement = connection.prepareStatement(sql);
                statement.setString(1, github);
                statement.setInt(2, userId);
            }else {
                // 是更新照片信息
                sql = "update user set photo='"+ user.getPhoto() + "' where userId=?";
                statement = connection.prepareStatement(sql);
                statement.setInt(1, userId);
            }
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 注册用户
    public void enroll(User user){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into user values(null,'"+user.getUsername()+"','"+user.getPassword()+"','"+user.getPhoto()+"',?)";
            String github = user.getGithub();
            if (github == null || github.equals("")){
                github = null;
            }
            statement = connection.prepareStatement(sql);
            statement.setString(1, github);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 根据用户id删除用户
    // 注册用户
    public void deleteUser(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from user where userId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }
}
